package com.catering.controller;

import com.catering.common.core.domain.R;
import com.catering.domain.OrderDishes;
import com.catering.domain.Orderr;
import com.catering.project.domain.Cart;
import com.catering.project.service.impl.CartServiceImpl;
import com.catering.service.impl.OrderrServiceImpl;
import com.catering.service.impl.OrderDishesServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderrServiceImpl orderrService;

    @Autowired
    private OrderDishesServiceImpl orderDishesService;
    @Autowired
    private CartServiceImpl cartService;

    @PostMapping("/addOrder")
    @PreAuthorize("@ss.hasPermi('order:add')")
    public R addOrder(@RequestBody Orderr order){
        orderrService.addOrder(order);
        List<Cart> carts=order.getDishes();
        for(Cart cart:carts){
            OrderDishes orderDishes=new OrderDishes();
            orderDishes.setOrderId(order.getId());
            orderDishes.setDishesId(Math.toIntExact(cart.getDishId()));
            orderDishes.setNum(Math.toIntExact(cart.getQuantity()));
            orderDishesService.addOderDish(orderDishes);
//              删除购物车数据
            cartService.deleteCartByCartId(cart.getCartId());
        }
        return R.ok("订单添加成功");
    }
}
